package com.open.system.dispatch.impl;

import com.open.base.utils.UserHolderUtils;
import com.open.base.model.LoginUser;
import com.open.base.model.User;
import com.open.system.dispatch.IOnlineUserDispatch;
import com.open.system.model.OnlineUser;
import com.open.common.model.Page;
import com.open.common.utils.CollectionUtils;
import com.open.common.utils.RedisUtils;
import com.open.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

import static com.open.common.constants.Constants.USER_REDIS;


/**
 * @author dashu03
 * @date 2025年07月23日 17:31
 * @description
 */
@Component(value = "homeDispatch")
public class OnlineUserDispatchImpl implements IOnlineUserDispatch {

    public static final long MIN_CURRENT_USER = 1;

    public static final long MAX_CURRENT_USER = 10000;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Boolean forceLogout(String token) {
        return redisUtils.deleteObject(USER_REDIS + token);
    }

    @Override
    public Page<OnlineUser> findPage(Page<OnlineUser> page, OnlineUser model) {
        List<OnlineUser> list = Page.startPage(this.getOnlineUsers(model), page);
        page.setItems(list);
        return page;
    }

    @Override
    public List<OnlineUser> getOnlineUsers(OnlineUser model) {
        Set<String> keys = redisUtils.scanKeys(USER_REDIS + "*", 1000);
        if (CollectionUtils.isEmpty(keys)) {
            return Collections.emptyList();
        }
        List<OnlineUser> lists = new ArrayList<>();
        for (String key : keys) {
            OnlineUser onlineUser = new OnlineUser();
            LoginUser loginUser = redisUtils.getCacheObject(key);
            User user = loginUser.getUser();

            // 搜索条件过滤
            if (model != null) {
                // 账号搜索
                if (StringUtils.isNotEmpty(model.getUsername())) {
                    if (loginUser.getUser().getUsername() == null ||
                            !loginUser.getUser().getUsername().contains(model.getUsername())) {
                        continue;
                    }
                }

                // 姓名搜索
                if (StringUtils.isNotEmpty(model.getName())) {
                    if (loginUser.getUser().getName() == null ||
                            !loginUser.getUser().getName().contains(model.getName())) {
                        continue;
                    }
                }
            }

            BeanUtils.copyProperties(user, onlineUser);
            onlineUser.setUid(loginUser.getToken());
            if (Objects.equals(UserHolderUtils.getToken(), loginUser.getToken())) {
                onlineUser.setCurrentUser(true);
            }
            lists.add(onlineUser);
        }
        return lists;
    }

    @Override
    public long onlineCount() {
        return redisUtils.scanCount(USER_REDIS + "*", 1000);
    }
}
